﻿using System;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Media;
using log4net;
using Stylet;

namespace VirtualWall.ViewModels
{
    public class ShellViewModel : Screen
    {
        private static readonly ILog log = LogManager.GetLogger("Monitor");
        public string _ProvicerTitle;
        public string ProvicerTitle {
            get
            {
                return _ProvicerTitle;
            }
            set
            {
                _ProvicerTitle =value;
                NotifyOfPropertyChange("ProvicerTitle");
            }
        }

        public bool WindowTopmost { get; set; } = false;

        private int _Camera1Connected;
        private int _Camera2Connected;
        private int _Camera3Connected;
        private int _Camera4Connected;

        public int Camera1Connected
        {
            get
            {
                return _Camera1Connected;
            }
            set
            {
                _Camera1Connected = value;

            }

        }

        public int Camera2Connected
        {
            get
            {
                return _Camera2Connected;
            }
            set
            {
                _Camera2Connected = value;

            }

        }

        public int Camera3Connected
        {
            get
            {
                return _Camera3Connected;
            }
            set
            {
                _Camera3Connected = value;

            }

        }

        public int Camera4Connected
        {
            get
            {
                return _Camera4Connected;
            }
            set
            {
                _Camera4Connected = value;
            }

        }
        public string _IP1;
        public string IP1 {

            get { 
                return _IP1;
            }
            set
            {
                _IP1 =$"相机：{value}";
                NotifyOfPropertyChange("IP1");
            }
        }
        public string _IP2;
        public string IP2
        {

            get
            {
                return _IP2;
            }
            set
            {
                _IP2 = $"相机：{value}";
                NotifyOfPropertyChange("IP2");
            }
        }
        public string _IP3;
        public string IP3
        {

            get
            {
                return _IP3;
            }
            set
            {
                _IP3 = $"相机：{value}";
                NotifyOfPropertyChange("IP3");
            }
        }

        public string _IP4;
        public string IP4
        {

            get
            {
                return _IP4;
            }
            set
            {
                _IP4 = $"相机：{value}";
                NotifyOfPropertyChange("IP4");
            }
        }

        public string _Plate1;
        public string Plate1
        {

            get
            {
                return _Plate1;
            }
            set
            {
                _Plate1 = $"识别到车牌：{value}";
                NotifyOfPropertyChange("Plate1");
            }
        }

        public string _Plate2;
        public string Plate2
        {

            get
            {
                return _Plate2;
            }
            set
            {
                _Plate2 = $"识别到车牌：{value}";
                NotifyOfPropertyChange("Plate2");
            }
        }

        public string _Plate3;
        public string Plate3
        {

            get
            {
                return _Plate3;
            }
            set
            {
                _Plate3 = $"识别到车牌：{value}";
                NotifyOfPropertyChange("Plate3");
            }
        }

        public string _Plate4;
        public string Plate4
        {

            get
            {
                return _Plate4;
            }
            set
            {
                _Plate4 = $"识别到车牌：{value}";
                NotifyOfPropertyChange("Plate4");
            }
        }

        public ShellViewModel()
        {
            Task.Run(async delegate
            {
                await Task.Delay(1000);

                try
                {
                    FlowControl.Instance.TryRun();
                    log.Info("程序启动成功！");

                    var camera1 = FlowControl.Instance.Cameras.Find(f => f.Number == 1);
                    if (camera1 != null)
                    {
                        camera1.OnResult += Camera_OnResult;
                        camera1.OnPlay += Camera_OnPlay;
                        camera1.Play(Globalspace._Wnd1Handle);
                    }

                    var camera2 = FlowControl.Instance.Cameras.Find(f => f.Number == 2);
                    if (camera2 != null)
                    {
                        camera2.OnResult += Camera_OnResult;
                        camera2.OnPlay += Camera_OnPlay;
                        camera2.Play(Globalspace._Wnd2Handle);
                    }

                    var camera3 = FlowControl.Instance.Cameras.Find(f => f.Number == 3);
                    if (camera3 != null)
                    {
                        camera3.OnResult += Camera_OnResult;
                        camera3.OnPlay += Camera_OnPlay;
                        camera3.Play(Globalspace._Wnd3Handle);
                    }

                    var camera4 = FlowControl.Instance.Cameras.Find(f => f.Number == 4);
                    if (camera4 != null)
                    {
                        camera4.OnResult += Camera_OnResult;
                        camera4.OnPlay += Camera_OnPlay;
                        camera4.Play(Globalspace._Wnd4Handle);
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
            });

            Task.Run(() =>
            {
                try
                {
                    //以下是服务商接口访问
                    var providerDetails = Common.SyncData.GetServiceProvider();
                    var Provicer = providerDetails;
                    ProvicerTitle = $"{providerDetails.service_products}  |  {providerDetails.company_name}  服务热线：{providerDetails.mobile}";

                    log.Debug("服务商接口访问成功！");
                }
                catch (Exception e)
                {
                    log.Debug($"服务商接口访问Error:{e.Message}！");
                }
            });
        }

        private void Camera_OnPlay(int index, Machines.Camera sneder)
        {
            if (sneder != null)
            {
                switch (index)
                {
                    case 1:
                        {
                            IP1 = $"{sneder.IP}";
                        }
                        break;
                    case 2:
                        {
                            IP2 = $"{sneder.IP}";
                        }
                        break;
                    case 3:
                        {
                            IP3 = $"{sneder.IP}";
                        }
                        break;
                    case 4:
                        {
                            IP4 = $"{sneder.IP}";
                        }
                        break;
                }
            }
        }

        private void Camera_OnResult(int index, Machines.Camera sneder)
        {
            if (sneder!=null)
            {
                switch (index)
                {
                    case 1:
                        {
                            Plate1 = $"{sneder.PlateNo}";
                        }
                        break;
                    case 2:
                        {
                            Plate2 = $"{sneder.PlateNo}";
                        }
                        break;
                    case 3:
                        {
                            Plate3 = $"{sneder.PlateNo}";
                        }
                        break;
                    case 4:
                        {
                            Plate4 = $"{sneder.PlateNo}";
                        }
                        break;
                }
            }
        }

        public void OpenBarrier(string index)
        {
            var camera = FlowControl.Instance.Cameras.Find(f => f.Number == int.Parse(index));
            if (camera != null)
            {
                camera.OpenBarrier();
            }
        }

        public void CloseBarrier(string index)
        {
            var camera = FlowControl.Instance.Cameras.Find(f => f.Number == int.Parse(index));
            if (camera != null)
            {
                camera.CloseBarrier();
            }
        }

        protected override void OnClose()
        {
            FlowControl.Instance.Dispose();
        }

        public void ShowVersion()
        {

        }

    }
}
